package com.weixin.paleituo.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.base.BaseRecycleAdapter;
import com.weixin.paleituo.bean.VisterRecordBean;
import com.weixin.paleituo.help.RecycleViewHolder;
import com.weixin.paleituo.util.StringUtils;
import com.weixin.paleituo.util.UIUtils;

import java.util.List;

/**
 * Author yichao
 * Time  2018/2/8 11:02
 * Dest  到访记录
 */

public class VisterRecordRecycleAdapter extends BaseRecycleAdapter<VisterRecordBean> {

    private TextView mTextViewHouseName,mTextViewId,mTextViewPerson,mTextViewTime,mTextViewMoney,mTextViewPosition;
    private LinearLayout mLlTagsRootView;
    private ImageView mImageViewIcon;
    private TextView mTvContact;

    public VisterRecordRecycleAdapter(Context context, List<VisterRecordBean> datas) {
        super(context, datas, R.layout.item_vister_record);
    }

    @Override
    protected void setData(RecycleViewHolder holder, final VisterRecordBean  visterRecordBean, int position) {
        mTextViewTime = holder.getItemView(R.id.tv_time_item_vister_record);
        mTextViewPerson = holder.getItemView(R.id.tv_person_item_vister_record);
        mTextViewId = holder.getItemView(R.id.tv_id_item_vister_record);
        mImageViewIcon = holder.getItemView(R.id.iv_icon_item_vister_record);
        mTextViewHouseName = holder.getItemView(R.id.tv_house_name_item_vister_record);
        mLlTagsRootView = holder.getItemView(R.id.ll_tags_item_vister_record);
        mTextViewMoney = holder.getItemView(R.id.tv_money_item_vister_record);
        mTextViewPosition= holder.getItemView(R.id.tv_position_item_vister_record);
        mTvContact = holder.getItemView(R.id.tv_contact_item_vister_record);

        mTextViewTime.setText(visterRecordBean.getVisitTime()+"");
        VisterRecordBean.ReceptionBean reception = visterRecordBean.getReception();
        if (reception!=null){
        mTextViewPerson.setText(""+reception.getName());
            mTextViewId.setText(""+reception.getJobNumber());
      final       String phone = reception.getPhone();

            if (!StringUtils.isEmpty(phone)){
                mTvContact.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            Intent dialIntent =  new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + phone));
                            mContext.startActivity(dialIntent);
                        }catch (Exception e){

                        }


                    }
                });
            }
        }




            Glide.with(mContext).load(visterRecordBean.getImgurl()+"").into(mImageViewIcon);
            mTextViewHouseName.setText(visterRecordBean.getHouse()+"");
            mTextViewPosition.setText(visterRecordBean.getPeriphery()+"");
            mTextViewMoney.setText(visterRecordBean.getMoney()+"");
            List<String> tagList = visterRecordBean.getTag();
            int tagPosition=-1;
            if (tagList!=null && tagList.size()>0){
                for (String tag:tagList){
                    if (!StringUtils.isEmpty(tag)){
                        tagPosition++;
                        if (tagPosition<=2){
                            TextView textView=new TextView(mContext);
                            if (tagPosition==0){
                                textView.setTextColor(UIUtils.getColor(R.color.c_ff37f4c5));
                                textView.setBackgroundResource(R.drawable.tag_1_shape);

                            }else if (tagPosition==1){
                                textView.setTextColor(UIUtils.getColor(R.color.c_25a6e9));
                                textView.setBackgroundResource(R.drawable.tag_2_shape);
                                LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                                layoutParams.setMargins(UIUtils.dip2px(15),0,0,0);
                                textView.setLayoutParams(layoutParams);

                            }else if (tagPosition==2){
                                textView.setTextColor(UIUtils.getColor(R.color.c_d84cfb));
                                textView.setBackgroundResource(R.drawable.tag_3_shape);
                                LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                                layoutParams.setMargins(UIUtils.dip2px(15),0,0,0);
                                textView.setLayoutParams(layoutParams);
                            }

                            textView.setPadding(UIUtils.dip2px(5),UIUtils.dip2px(2),UIUtils.dip2px(5),UIUtils.dip2px(2));
                            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,UIUtils.px2dip(UIUtils.getDimens(R.dimen.sp_12)));
                            textView.setGravity(Gravity.CENTER);
                            textView.setText(tag);
                            mLlTagsRootView.addView(textView);
                        }
                    }
                }

            }



        holder.mItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //  HouseDetailsActivity.startActivity(mContext,visterRecordBean.getHouses().getId()+"");
            }
        });

    }
}
